/*
Spotter Exercise Manager
Copyright 2011 Evan Nelson.

This file is part of Spotter Exercise Manager.

Spotter Exercise Manager is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Spotter Exercise Manager is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Spotter Exercise Manager.  If not, see <http://www.gnu.org/licenses/>.

The author can be contacted by emailing ean5533@gmail.com
*/

#include <QApplication>
#include <QDesktopWidget>
#include <QRect>
#include <QPrintDialog>
#include <QPrinter>
#include <QPainter>
#include <QFileDialog>
#include <QPixmap>

#include "plot.h"
#include "ui_plot.h"

#include "plotter.h"

Plot::Plot(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Plot)
{
    ui->setupUi(this);

    //don't let the program keep running if only graph windows are still open
    this->setAttribute(Qt::WA_QuitOnClose, false);

    //initially set myPlot to be some empty graph
    Plotter::buildEmptyPlot(ui->plot);

    //center on screen
    centerWidgetOnScreen(this);
}

Plot::~Plot()
{
    delete ui;
}

void Plot::centerWidgetOnScreen(QWidget* widget) {
     QRect rect = QApplication::desktop()->availableGeometry(widget);
     widget->move(rect.center() - widget->rect().center());
}

void Plot::showWtOverTimePlot(QStringList exercises, QDate *minDate, QDate *maxDate) {
    Plotter::buildWtOverTimePlot(ui->plot, true, exercises, minDate, maxDate);
}

void Plot::changeEvent(QEvent *e)
{
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void Plot::on_pushButton_clicked()
{
    QPrinter printer;

    QPrintDialog *dialog = new QPrintDialog(&printer, this);
    dialog->setWindowTitle(tr("Print graph"));
    if (dialog->exec() != QDialog::Accepted) return;

    ui->plot->print(printer);
}

void Plot::on_pushButton_2_clicked()
{
    QString fileName = QFileDialog::getSaveFileName(this, "Save graph to file", QDir::homePath());
    if(fileName.isEmpty()) return;

    QPixmap graph(ui->plot->size());
    graph.fill();
    ui->plot->print(graph);

    graph.save(fileName);
}
